<?php
	session_start();
	class RegionesManager{
		public static function Add($nombre){

			$db = DataBase::getInstance();
			$db->execute("INSERT INTO regiones(nombre) VALUES ('".$nombre."')");
			$last_inserted_id = $db->last_inserted_id();
			return self::Get($last_inserted_id);
		}
		
		public static function Update($id,$nombre){
			$db = DataBase::getInstance();
			$db->execute("UPDATE regiones SET nombre = '".$nombre."'  where id_region = ". $id);
		}
		
		public static function GetAll(){
			$db = DataBase::getInstance();
			$db->setQuery("SELECT id_region id, nombre FROM regiones ORDER BY nombre ASC");
			$list = $db->loadObjectList();
			return $list;
		}


		public static function GetForXml(){
			$db = DataBase::getInstance();
			$db->setQuery("SELECT id_region id, nombre FROM regiones ORDER BY nombre ASC");
			$list = $db->loadObjectList();
			return $list;
		}
		
		public static function Get($id){
			$db = DataBase::getInstance();
			$db->setQuery("SELECT id_region id, nombre FROM regiones WHERE id_region = ".$id);
			$list = $db->loadObjectList();
			return $list[0];
		}

		public static function Delete($id){
			$db = DataBase::getInstance();
			$db->execute("DELETE FROM regiones WHERE id_region = ".$id);
		}
		
		public static function GetIdByName($nombre){
			if($nombre == "" || $nombre == "x")return 'nulla';
			$db = DataBase::getInstance();
			$db->setQuery("SELECT id_region id FROM regiones WHERE nombre = '".trim($nombre)."'");

			$list = $db->loadObjectList();
			if(count($list)==0){
				$list = self::Add(trim($nombre));
						}else{
				$list = $list[0];
			}
			if($list->id != ""){
				return $list->id;
			}else{
				return 'null';
			}
		}

	}
?>